package com.example.rfidserver.dao;

import android.content.ContentValues;
import android.content.SyncStatusObserver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.rfidserver.pojo.User;

import java.util.ArrayList;
import java.util.List;

/*
*   查询对应的信息
* */
public class UserDao {




    //获取用户所有信息
    public List<User> getUserInfo(){
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db",null,SQLiteDatabase.OPEN_READONLY);
        Cursor cursor = db.query("user",null, null, null, null, null, null);
        List users = new ArrayList();
        while(cursor.moveToNext()){
            long id = cursor.getLong(0);
            String r_id = cursor.getString(1);
            int ticket_id = cursor.getInt(2);
            double integral = cursor.getDouble(3);
            int role = cursor.getInt(4);
            int consume_id = cursor.getInt(5);
            User user = new User();
            user.setId(id);
            user.setIntegral_bill(integral);
            user.setTicket_id(ticket_id);
            user.setR_id(r_id);
            user.setRole(role);
            user.setConsume_id(consume_id);
            users.add(user);
        }
        db.close();
        return  users;
    }

    //根据用户的身份证号得到用户积分信息
    public User getUserIntegralByID(long id){
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db",null,SQLiteDatabase.OPEN_READONLY);
        Cursor cursor = db.rawQuery("select * from user where id = ?",new String[]{String.valueOf(id)});
        User user = new User();
        while(cursor.moveToNext()){
            double integral = cursor.getDouble(3);
            user.setIntegral_bill(integral);
        }
        db.close();
        return  user;
    }


    public int deleteUserByID(long id){
        //权限设置为可都可写
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db",null,SQLiteDatabase.OPEN_READWRITE);
        ContentValues cv = new ContentValues();
        String[] args = {String.valueOf(id)};
        int res = db.delete("user", "id=?", args);
        db.close();
        return res;
    }


    //传递过来一个consume对象，我要更新它
    public int updateOneUserByID(User user){
        long user_id = user.getId();
        //权限设置为可都可写
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db",null,SQLiteDatabase.OPEN_READWRITE);
        ContentValues cv = new ContentValues();
        /*CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,r_id INTEGER,ticket_id INTEGER,integral_bill REAL,roll INTEGER,consume_Id INTEGER,FOREIGN KEY (ticket_id) REFERENCES ticket(ticket_id) ON DELETE CASCADE ON UPDATE CASCADE
FOREIGN KEY (consume_Id) REFERENCES consume(consume_Id) ON DELETE CASCADE ON UPDATE CASCADE);*/
        cv.put("r_id",user.getR_id());
        cv.put("ticket_id",user.getTicket_id());
        cv.put("integral_bill",user.getIntegral_bill());
        cv.put("consume_Id",user.getConsume_id());
        cv.put("ticket_id",user.getTicket_id());
        String[] args = {String.valueOf(user_id)};
        int res = db.update("user",cv,"id=?",args);
        db.close();
        return res;
    }

    /*传递过来一个卡号和服务号
    * */
    public void updateUserIntegralByR_ID(String R_ID,String service_ID){
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db",null,SQLiteDatabase.OPEN_READWRITE);
        String sql = "update user set integral_bill = integral_bill - " +
                "(select consume_Item_integral from consume where consume_Id = " + Integer.parseInt(service_ID)
                + ") where r_id = '" + R_ID + "';";
        db.execSQL(sql);
        db.close();
        return;
    }

    public void updateVolunteerByID(long id){
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db",null,SQLiteDatabase.OPEN_READWRITE);
        String sql = "update user set integral_bill = integral_bill + 80.0 where id = " + id;
        db.execSQL(sql);
        return;
    }

}
